package com.ruipengkj.component;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.TexturePaint;

import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.plaf.metal.MetalTextFieldUI;

import com.ruipengkj.commons.util.Util;
import com.ruipengkj.factory.ImageFactory;

public class RPJTextField extends JTextField{
	private static final long serialVersionUID = 1L;
	private ImageIcon backgroundImageIcon = ImageFactory.loadImage(ImageFactory.TEXT_BG);
	private TexturePaint paint = Util.createTexturePaint(backgroundImageIcon.getImage());
	private Image backgroundLeftImage = ImageFactory.loadImage(ImageFactory.TEXT_LEFT_BG).getImage();
	private Image backgroundRightImage = ImageFactory.loadImage(ImageFactory.TEXT_RIGHT_BG).getImage();
	
	public RPJTextField(int width){
		super(width);
		this.init();
	}
	
	

	private void init() {

		this.setUI(new MetalTextFieldUI() {
			@Override
			protected void paintBackground(Graphics g) {
				Graphics2D g2d = (Graphics2D) g;
				// fill background.
				g2d.setPaint(paint);
				g2d.fillRect(0, 0, getWidth(), getHeight());
				// draw left image.
				g2d.drawImage(backgroundLeftImage, 0, 0, null);
				// draw right image.
				int x = getWidth() - backgroundRightImage.getWidth(null);
				int y = 0;
				g2d.drawImage(backgroundRightImage, x, y, null);
			}

		});
	}
}
